package com.sxtyxt.online;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;


public class GuideActivity extends ActBase {

	private ViewPager viewPager;
	private List<View> imageViews;
	private List<ImageView> dots;

    private final int PICNUM=3;
    private int x;

	@Override
	protected int getLayoutId() {
		return R.layout.guide;
	}

	@Override
	protected void initUI() {
		super.initUI();
		getWindow().setBackgroundDrawable(null);

		imageViews = new ArrayList<>();
		LayoutInflater inflater = getLayoutInflater();
		View pager1 = inflater.inflate(R.layout.guide_one, null);
		View pager2 = inflater.inflate(R.layout.guide_two, null);
		View pager3 = inflater.inflate(R.layout.guide_three, null);

		imageViews.add(pager1);
		imageViews.add(pager2);
		imageViews.add(pager3);

		dots = new ArrayList<>();
		dots.add((ImageView) findViewById(R.id.v_dot0));
		dots.add((ImageView) findViewById(R.id.v_dot1));
		dots.add((ImageView) findViewById(R.id.v_dot2));

		viewPager = (ViewPager) findViewById(R.id.vp);
		viewPager.setAdapter(new MyAdapter());
		viewPager.setOnPageChangeListener(new MyPageChangeListener());

		pager3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				SystemEnv.setFirstRun(false);
				Intent intent;
				intent = new Intent(GuideActivity.this, ActLogin.class);
				GuideActivity.this.startActivity(intent);
				finish();
			}
		});

		viewPager.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (oldPosition != PICNUM - 1)
					return false;
				switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						x = (int) event.getX();
						break;
					case MotionEvent.ACTION_MOVE:
						if (x - event.getX() > 100) {

							SystemEnv.setFirstRun(false);
							Intent intent;
							intent = new Intent(GuideActivity.this, ActLogin.class);
							GuideActivity.this.startActivity(intent);
							finish();
						}
						break;

					default:
						break;
				}
				return false;
			}
		});
	}
	private int oldPosition = 0;


	private class MyPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			dots.get(oldPosition).setImageResource(R.drawable.dot_normal);
			dots.get(arg0).setImageResource(R.drawable.dot_selected);
			oldPosition = arg0;
		}

	}


	private class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return imageViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(imageViews.get(arg1));
			return imageViews.get(arg1);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

		@Override
		public void finishUpdate(View arg0) {

		}
	}

	boolean isExit = false;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	public void exit() {
		if (!isExit) {
			isExit = true;
			doToast("再按一次退出程序");
			mHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			SystemEnv.systemOut();
		}
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			isExit = false;
		}

	};

}
